package qr.game.rest;

import java.util.ArrayList;

import qr.core.*;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

public class RestClient {
	private String ipAddress;
	private int port;
	private String baseUri;
	private String protocol;
	private Client client;

	public RestClient(String ipAddress, int port) {
		this.ipAddress = ipAddress;
		this.port = port;
		this.protocol = "http://";
		this.baseUri = protocol + ipAddress + ":" + port;
		
		client = Client.create();
		client.setFollowRedirects(true);
	}

	/**
	 * GET Method Returns Game Information
	 * 
	 */
	public Game getGameInfo() {
		WebResource r = client.resource(baseUri + "/game");
		Game response = null;
		try { 
			response = r.get(Game.class);
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Get game info failed");		
		}
		return response;
	}

	/**
	 * GET Method Returns Leaderboard Data
	 * 
	 */
	public Leaderboard getLeaderboard() {
		WebResource r = client.resource(baseUri + "/game/leaderboard");
		Leaderboard response = null;
		try { 
			response = r.get(Leaderboard.class);
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Get leaderboard info failed");		
		}
		return response;
	}

	/**
	 * GET Method Returns list of all locations
	 * 
	 */
	public ArrayList<Location> getLocations() {
		WebResource r = client.resource(baseUri + "/game/location");
		ArrayList response = null;
		try { 
			response = r.get(ArrayList.class);
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Get locations failed");		
		}
		
		return (ArrayList<Location>)(response);
	}

	/**
	 * POST Method Registers a new user
	 * 
	 */
	public boolean registerUser(String userId, Player userData) {
		WebResource r = client.resource(baseUri + "/game/location/" + userId);
		ClientResponse response = null;
		try { 
			response = r.post(ClientResponse.class , userData );
			return true;
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Enter location failed");	
			return false;
		}
	}

	/**
	 * GET Method Gets information about a single location.
	 */
	public Location getLocationInfo(String locationId) {
		WebResource r = client.resource(baseUri + "/game/leaderboard/" + locationId);
		Location response = null;
		try { 
			response = r.get(Location.class);
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Get lacation info failed");		
		}
		return response;
	}

	/**
	 * GET Method Get's a user's found locations
	 */
	public ArrayList<Location> getFoundLocations(String userId) {
		WebResource r = client.resource(baseUri + "/game/location/" + userId);
		ArrayList response = null;
		try { 
			response = r.get(ArrayList.class);
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Get locations failed");		
		}
		
		return (ArrayList<Location>)(response);
	}

	/**
	 * POST Method Enters a location for specified user.
	 */
	public boolean enterLocation(String userId, String qrId) {
		WebResource r = client.resource(baseUri + "/game/location/" + userId);
		ClientResponse response = null;
		try { 
			response = r.post(ClientResponse.class , qrId);
			return true;
		} catch ( UniformInterfaceException ue ) {
			ClientResponse errorResponse = ue.getResponse();
			System.err.println("Enter location failed");	
			return false;
		}
	}

}
